﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Biz;
namespace MockPrjTeam2.Pages.Forms
{
    public partial class SupportingMaterialDetails : System.Web.UI.Page
    {
        private int supportingMaterialId;
        private SupportingMeterialBiz supportingMeterialBiz;
        private int organizationId;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!int.TryParse(Request.Params["id"], out supportingMaterialId))
            {
                Response.Redirect("../Views/ListOrganizations.aspx");
            }
            else
            {
                supportingMeterialBiz = new SupportingMeterialBiz();
            }

            if(!IsPostBack)
            {
                BindFields();
            }
            
            
           
        }
        private void BindFields()
        {
         //  SupportingMeterialBiz supportingMeterialBiz=new SupportingMeterialBiz();

      
           

            if (int.TryParse(Request.Params["id"], out supportingMaterialId))
            {
               
         
            SupportingMeterial suppportingMaterial = supportingMeterialBiz.GetSupportingById(supportingMaterialId);

            TextBoxURL.Text = suppportingMaterial.Url;
         
            TextBoxDescription.Text = suppportingMaterial.Description;
            TextBoxAddedBy.Text = suppportingMaterial.Account;
            TextBoxAddedDate.Text = suppportingMaterial.AddDate.ToString(CultureInfo.InvariantCulture);

            DropDownListType.SelectedValue = suppportingMaterial.Type;
            HiddenFieldOrgId.Value = suppportingMaterial.OrgId.ToString(CultureInfo.InvariantCulture);

            DropDownListType.SelectedValue = suppportingMaterial.Type;


            HiddenFieldUserId.Value = suppportingMaterial.UserID.ToString(CultureInfo.InvariantCulture);
            HiddenFieldSupportingMaterialId.Value =suppportingMaterial.SuportingMeterialId.ToString(CultureInfo.InvariantCulture);
            //  ButtonInactive.Visible = suppportingMaterial.IsActive;
            }
        }

      
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            SupportingMeterial supporting=new SupportingMeterial();
            supporting.Url = TextBoxURL.Text;
            supporting.Description = TextBoxDescription.Text;
            supporting.Type = DropDownListType.SelectedItem.ToString();
            supporting.SuportingMeterialId =Convert.ToInt32(HiddenFieldSupportingMaterialId.Value);
            int id =Convert.ToInt32(HiddenFieldOrgId.Value);
            bool result = supportingMeterialBiz.UpdateSupportingMaterial(supporting);
            if (result)
            {


                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                            "OrganizationDetails.aspx?id={0}#DetailsPanel05", id));


            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(HiddenFieldOrgId.Value);
            
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/OrganizationDetails.aspx?id={0}#DetailsPanel05",id));
            
        }

        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            int supportingMaterial;
            if (int.TryParse(Request.Params["id"], out supportingMaterial))
            {
               supportingMeterialBiz.SetSupportingMateriaInActive(supportingMaterial, false);
                ButtonInactive.Visible = false;
            }
        }
    }
}